package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseUtils {

    private static DatabaseUtils dbu = null;

    public static DatabaseUtils getInstance() throws SQLException,ClassNotFoundException {
        if (dbu == null) {
            dbu = new DatabaseUtils();
        }
        return dbu;
    }
    private Connection con = null;

    private DatabaseUtils() throws  SQLException,ClassNotFoundException {
        // buoc 1: nap driver
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        // mo ket noi
        con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=Electronic", "sa", "123456");

    }

    @Override
    public void finalize() throws Throwable {
        // dong ket noi Database
        try {
            if (con != null) {
                con.close();
            }
        } catch (SQLException e) {
            System.out.println("Error:" + e.getMessage());
            throw e;
        }
    }

    public Connection getConnection() {
        return con;
    }
}
